package com.joymart.common.abstraction;

import com.joymart.common.error.handling.JoymartBusinessException;

import java.util.Arrays;

public abstract class StatusService<T extends StatusHolder<S>, S extends Enum<S>> {


    /**
     * 检查业务单据当前状态是否合法
     * @param t
     * @param legalStatuses
     */
    public void checkStatus(T t, S... legalStatuses){
        if(t == null){
            throw new JoymartBusinessException("业务单据不存在");
        }
        boolean legal = Arrays.asList(legalStatuses).contains(t.getStatus());
        if(!legal){
            throw new JoymartBusinessException("当前业务单据状态不允许该操作");
        }
    }



    /**
     * 修改业务单据状态
     * 注意，并不持久化
     * @param t
     * @param target
     */
    public void modifyStatus(T t, S target){
        t.setStatus(target);
    }


}
